package com.example.common.plugins.authentication;

import com.example.starter.security.pojo.ExampleUser;
import com.example.starter.util.Response;
import com.example.system.dto.UserInfo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * @author 王令
 * @since 2022/7/29 18:05
 */
public interface ExampleUserInfoService {

    default UserDetails getUserDetailsInfo(Response<UserInfo> response) throws UsernameNotFoundException {
        if (response == null || response.getData() == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        UserInfo info = response.getData();
        return new ExampleUser()
                .setId(info.getId())
                .setUsername(info.getUsername())
                .setPhone(info.getPhone())
                .setPassword(info.getPassword())
                .setEnabled(info.getEnabled())
                .setNonLocked(info.getNonLocked())
                .setRoles(info.getRoles())
                .setAuthorities(info.getAuthorities());
    }
}
